package com.dhy.takeaway.service;

import com.dhy.takeaway.dao.AddressDao;
import com.dhy.takeaway.entity.Address;
import com.dhy.takeaway.entity.Res;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddressService {

    @Autowired
    AddressDao addressDao;

    public List<Address> findAddress(String buyerId){
        return addressDao.findAddress(buyerId);
    }

    public Res editAddress(Address address){
        Res res = new Res();

        if (address.getId() != null && address.getId() != "") {
            if (addressDao.updateAddress(address) > 0) {
                res.setCode("200");
                res.setMessage("修改成功！");
            } else {
                res.setCode("400");
                res.setMessage("修改失败！");
            }
        } else {
            if (addressDao.insertAddress(address) > 0) {
                res.setCode("200");
                res.setMessage("添加成功！");
            } else {
                res.setCode("400");
                res.setMessage("添加失败！");
            }
        }

        return res;
    }

    public Res deleteAddress(String id){
        Res res = new Res();
        if (addressDao.deleteAddress(id) > 0) {
            res.setCode("200");
            res.setMessage("删除成功！");
        } else {
            res.setCode("400");
            res.setMessage("删除失败！");
        }
        return res;
    }
}
